/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.entity.VBrandAgreementRenewal;
import com.ykht.proj.admin.api.entity.SysUser;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DelFlagEnum;
import com.ykht.proj.coupon.entity.FxSensitiveWords;
import com.ykht.proj.coupon.service.FxSensitiveWordsService;
import com.ykht.proj.coupon.vo.FxSensitiveWordsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 敏感词表
 *
 * @author proj code generator
 * @date 2021-07-07 16:20:00
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxsensitivewords" )
@Api(value = "fxsensitivewords", tags = "敏感词表管理")
@Slf4j
public class FxSensitiveWordsController {

    private final  FxSensitiveWordsService fxSensitiveWordsService;

	/**
	 * 用户 服务 Api
	 */
	private final RemoteUserService remoteUserService;

    /**
     * 敏感词表 分页查询
     * @param page 分页对象
     * @param wordsVo 敏感词表
     * @return
     */
    @ApiOperation(value = "敏感词表 分页查询", notes = "敏感词表 分页查询")
    @GetMapping("/page" )
    public R getFxSensitiveWordsPage(Page page, FxSensitiveWordsVo wordsVo) {
		log.info("->敏感词表 分页查询:{}", JSONUtil.toJsonStr(wordsVo));
		IPage<FxSensitiveWordsVo> iPage = fxSensitiveWordsService.getFxSensitiveWordsPage(page, wordsVo);
		List<FxSensitiveWordsVo> records = iPage.getRecords();
		if (null != records && records.size() > 0){
			List<Long> createUserList = records.stream().map(FxSensitiveWordsVo::getCreateUser).collect(Collectors.toList());
			R<List<SysUser>> userList = remoteUserService.getUserByIds(createUserList);
			records.stream().forEach(itme -> {
				userList.getData().stream().forEach(user ->{
					if (itme.getCreateUser().equals(Long.valueOf(user.getUserId()))) {
						itme.setCreateName(user.getUsername());
					}
				});
			});
			iPage.setRecords(records);
		}
		return R.ok(iPage);
    }


    /**
     * 通过id查询敏感词表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxsensitivewords_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fxSensitiveWordsService.getById(id));
    }

    /**
     * 新增敏感词表
     * @param fxSensitiveWords 敏感词表
     * @return R
     */
    @ApiOperation(value = "新增敏感词表", notes = "新增敏感词表")
    @SysLog("新增敏感词表" )
    @PostMapping
    public R save(@RequestBody FxSensitiveWords fxSensitiveWords) {
		fxSensitiveWords.setStatus(0);
		fxSensitiveWords.setCreateUser(SecurityUtils.getUserId());
		fxSensitiveWords.setCreateTime(LocalDateTime.now());
        return R.ok(fxSensitiveWordsService.save(fxSensitiveWords));
    }

	@ApiOperation(value = "校验 敏感词")
	@GetMapping("/checkContent")
	public R checkSjName(FxSensitiveWords fxSensitiveWords) {
		log.info("->校验 敏感词{}", fxSensitiveWords);
		FxSensitiveWords one = fxSensitiveWordsService.getOne(Wrappers.lambdaQuery(FxSensitiveWords.class)
				.eq(FxSensitiveWords::getContent, fxSensitiveWords.getContent())
				.eq(FxSensitiveWords::getDelFlag, DelFlagEnum.usable.getValue()));
		return R.ok(one == null ? true : one.getId().equals(fxSensitiveWords.getId()) ? true : false);
	}


	/**
     * 修改敏感词表
     * @param fxSensitiveWords 敏感词表
     * @return R
     */
    @ApiOperation(value = "修改敏感词表", notes = "修改敏感词表")
    @SysLog("修改敏感词表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxsensitivewords_edit')" )
    public R updateById(@RequestBody FxSensitiveWords fxSensitiveWords) {
        return R.ok(fxSensitiveWordsService.updateById(fxSensitiveWords));
    }

    /**
     * 通过id删除敏感词表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除敏感词表", notes = "通过id删除敏感词表")
    @SysLog("通过id删除敏感词表" )
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxSensitiveWordsService.removeById(id));
    }

}
